/*
 * Servidor.java
 *
 * Created on 4 de Junho de 2007, 21:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package servidor;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.JTextField;

/**
 *
 * @author bohrer
 */
public class Servidor extends Thread {
    private Vector<Cliente> listaClientes;
    private Vector<String> usuarios;
    private Vector<String> senhas;
    private boolean ativo;
    
    /** Creates a new instance of Main */
    public Servidor() {
        //this.ativo = false;sock
        this.ativo = false;
        this.listaClientes = new Vector<Cliente>();
        this.usuarios = new Vector<String>();
        this.senhas = new Vector<String>();
    }
    
    public synchronized void addUser(String name,String passwd) {
        int x;
        if ( (x=this.usuarios.indexOf(name)) > -1) {
            this.senhas.set(x,passwd);
        } else {
            this.usuarios.add(name);
            this.senhas.add(passwd);
        }
    }
    
    public boolean authenticateUser(String name,String passwd) {
        int x;
        if ( (x=this.usuarios.indexOf(name)) > -1) {
            return (this.senhas.get(x).equals(passwd));
        }
        return false;
    }
    
    public synchronized boolean delUser(String name) {
        int x;
        if ( (x=this.usuarios.indexOf(name)) > -1) {
            this.usuarios.remove(x);
            this.senhas.remove(x);
            return true;
        }
        return false;
    }
    
    public Object[] getUsers() {
        return this.usuarios.toArray();
    }
    
    public void up() {
        this.ativo = true;
        this.start();
    }
    
    public boolean isActive() {
        return this.ativo;
    }
    
    public void down() {
        this.ativo = false;
        try {
            new Socket("localhost",666);
        } catch (UnknownHostException ex) {
        } catch (IOException ex) {
        }
        System.gc();
    }
    
    public void removeClient(Cliente tc) {
        this.listaClientes.remove(tc);
    }
    
    public int getActiveConnections() {
        return this.listaClientes.size();
    }
    
    public synchronized void broadCast(byte[] b) {
        for(Cliente t:this.listaClientes) {
            try {
                t.send(b);
            } catch (IOException ex) {
                t.killClient();
            }
        }
    }
    
    public void run() {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(666);
            while (this.ativo) {
                Socket clientSocket = null;
                clientSocket = serverSocket.accept();
                Cliente tc;
                this.listaClientes.add(tc = new Cliente(clientSocket,this));
                tc.start();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

